package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author 金帮嘉诚 @date 2021-08-14 星期六 17:19
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;


    /**
     * @param pageResult 含三个参数 query查询条件 pageNum当前页 pageSize每页条数
     * @return pageResult 增加连个参数 商品分页数据rows 商品总数total
     */
    @Override
    public PageResult getItemList(PageResult pageResult) {
        int pageNum = pageResult.getPageNum();
        int pageSize = pageResult.getPageSize();
        //获取page对象
        Page<Item> page = new Page<>(pageNum, pageSize);
        //设置查询条件
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        boolean queryFlag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(queryFlag, "title", pageResult.getQuery());
        page = itemMapper.selectPage(page, queryWrapper);
        //设置pageResult的成员变量
        pageResult.setRows(page.getRecords());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }
}
